package com.grow.greenhouse.controller;

import com.grow.greenhouse.common.BaseResponse;
import com.grow.greenhouse.common.ErrorCode;
import com.grow.greenhouse.domain.User;
import com.grow.greenhouse.domain.request.AddMemberRequest;
import com.grow.greenhouse.domain.request.EmailRequest;
import com.grow.greenhouse.domain.request.UserLoginRequest;
import com.grow.greenhouse.domain.request.UserRegisterRequest;
import com.grow.greenhouse.exception.BusinessException;
import com.grow.greenhouse.service.UserService;
import com.grow.greenhouse.utils.JwtUtil;
import com.grow.greenhouse.utils.MailUtil;
import com.grow.greenhouse.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.grow.greenhouse.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping("/email/verify")
    public BaseResponse<String> emailVerify(@RequestBody EmailRequest email)
    {
        if(StringUtils.isBlank(email.getEmail())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"邮箱账号不得为空");
        }
        userService.sendEmailVerifyCode(email.getEmail());
        return ResultUtils.success("发送成功");
    }



    @PostMapping("/register")
    public BaseResponse<Map<String,Object>> userRegister(@RequestBody UserRegisterRequest userRegisterRequest)
    {
        if(userRegisterRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"传入登录参数为空");
        }
        String userName = userRegisterRequest.getUserName();
        String userVeryCode = userRegisterRequest.getUserVeryCode();
        String email = userRegisterRequest.getUserAccount();
        String password = userRegisterRequest.getUserPassword();
        if(StringUtils.isAnyBlank(userName,userVeryCode,email,password))
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号/验证码/密码不能为空");
        }
        Map<String, Object> map = userService.userRegister(userName, userVeryCode, email, password);
        return ResultUtils.success(map);
    }

    @PostMapping("/login")
    public BaseResponse<Map<String,Object>> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request)
    {
        if(userLoginRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"传入登录参数为空");
        }
        String account = userLoginRequest.getUserAccount();
        String password = userLoginRequest.getUserPassword();
        if(StringUtils.isAnyBlank(account,password)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号或密码不能为空");
        }

        Map<String, Object> map = userService.userLogin(account, password, request);

        return ResultUtils.success(map);
    }

    @Deprecated
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        if(request == null)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        int id = userService.userLogout(request);
        return ResultUtils.success(id);

    }
    @GetMapping("/current")
    public BaseResponse<User> currentUser(HttpServletRequest request)
    {

        User currentUser = userService.getLoginUser(request);
        if(currentUser == null)
        {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        User safeUser = userService.getSafeUser(currentUser);
        return ResultUtils.success(safeUser);


    }

    @GetMapping("/get/members")
    public BaseResponse<Map<String,List<User>>> getMembers(HttpServletRequest request)
    {
        Map<String,List<User>> userList = userService.getMembers(request);
        return ResultUtils.success(userList);
    }

    @PostMapping("/add/member")
    public BaseResponse<String> addMembers(@RequestBody AddMemberRequest addMemberRequest
            , HttpServletRequest request)
    {
        String add = userService.addMembers(addMemberRequest,request);
        return ResultUtils.success(add);
    }
}
